package kr.hkit.bsnsalemgn.inputUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import kr.hkit.bsnsalemgn.Config;
import kr.hkit.framework.inputPanel.InputCmbPanel;
import kr.hkit.framework.inputPanel.InputTxtPanel;
import kr.hkit.framework.inputPanel.ValidCheck;


public abstract class AbsInputBaseUI extends JPanel implements ActionListener {
	int fieldIdx;	// Config.INPUT_FIELD_NAME[]에 저장된 필드명
					/*  0 : { "사원코드", "사원명" },
						1 : { "제품코드", "제품명", "판매정가", "판매원가" }, 
						2 : { "거래처코드", "거래처명" }*/
	int comboIdx;	// Config.INPUT_COMBO_NAME[]에 저장된 콤보 필드명
					// 0 : "사원등급",  1 : "거래처등급", -1 : 콤보상자없음
	
	InputTxtPanel[] inputTfComps;
	InputCmbPanel inputCombo;
	JButton btnSave;
	JButton btnDel;
	JButton btnSearch;

	public AbsInputBaseUI(int fiedIdx, int comboIdx) {
		this.fieldIdx = fiedIdx;
		this.comboIdx = comboIdx;
		initialize();
	}
	
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panelMain = new JPanel();
		add(panelMain, BorderLayout.CENTER);
		panelMain.setLayout(new GridLayout(0, 1, 0, 0));
		
		inputTfComps = new InputTxtPanel[Config.INPUT_FIELD_NAME[fieldIdx].length];
		
		for(int i=0; i<inputTfComps.length; i++){
			inputTfComps[i] = new InputTxtPanel(Config.INPUT_FIELD_NAME[fieldIdx][i]);
			inputTfComps[i].setPreferredSize(new Dimension(300, 60));
			panelMain.add(inputTfComps[i]);
		}
		
		if (comboIdx>-1){
			inputCombo = new InputCmbPanel(Config.INPUT_COMBO_NAME[comboIdx]);
			inputCombo.setComboItems(Config.INPUT_COMBO_LIST[comboIdx]);
			inputCombo.setPreferredSize(new Dimension(300, 60));
			panelMain.add(inputCombo);
		}
		
		JPanel panelBtn = new JPanel();
		add(panelBtn, BorderLayout.SOUTH);
		
		btnSave = new JButton("저장");
		btnSave.addActionListener(this);
		panelBtn.add(btnSave);
		
		btnDel = new JButton("삭제");
		btnDel.addActionListener(this);
		panelBtn.add(btnDel);
		
		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panelBtn.add(btnSearch);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSearch) {
			btnSearchActionPerformed(e);
		}
		if (e.getSource() == btnDel) {
			btnDelActionPerformed(e);
		}
		if (e.getSource() == btnSave) {
			btnSaveActionPerformed(e);
		}
	}
	
	protected void clearComp(){
		for(int i=0; i<Config.INPUT_FIELD_NAME[fieldIdx].length; i++){
			inputTfComps[i].setTfValue("");
		}
		if (inputCombo!=null){
			inputCombo.setDefaultSelectIndex(0);
		}
	}
	
	protected void validCheck() throws Exception{
		for(int i=0; i<Config.INPUT_FIELD_NAME[fieldIdx].length; i++){
			try {
				ValidCheck.isValidValue(inputTfComps[i]);
			} catch (Exception e) {
				throw new Exception(Config.INPUT_FIELD_NAME[fieldIdx][i] + e.getMessage());
			}
		}
	}
	protected abstract void btnSaveActionPerformed(ActionEvent e);
	protected abstract void btnDelActionPerformed(ActionEvent e);
	protected abstract void btnSearchActionPerformed(ActionEvent e);
}
